{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.输出9x9乘法口诀表。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1×1=1\t\n",
      "1×2=2\t2×2=4\t\n",
      "1×3=3\t2×3=6\t3×3=9\t\n",
      "1×4=4\t2×4=8\t3×4=12\t4×4=16\t\n",
      "1×5=5\t2×5=10\t3×5=15\t4×5=20\t5×5=25\t\n",
      "1×6=6\t2×6=12\t3×6=18\t4×6=24\t5×6=30\t6×6=36\t\n",
      "1×7=7\t2×7=14\t3×7=21\t4×7=28\t5×7=35\t6×7=42\t7×7=49\t\n",
      "1×8=8\t2×8=16\t3×8=24\t4×8=32\t5×8=40\t6×8=48\t7×8=56\t8×8=64\t\n",
      "1×9=9\t2×9=18\t3×9=27\t4×9=36\t5×9=45\t6×9=54\t7×9=63\t8×9=72\t9×9=81\t\n"
     ]
    }
   ],
   "source": [
    "for i in range(1, 10):\n",
    "    for j in range(1, i + 1):\n",
    "        print(f\"{j}×{i}={i * j}\", end=\"\\t\")\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.输入一个任意正整数，输出分解质因数。例如：输入90,打印出90=2*3*3*5。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4 = 2* 2"
     ]
    }
   ],
   "source": [
    "num = int(input(\"请输入一个正整数: \"))\n",
    "print(num, \"=\", end=\" \")\n",
    "\n",
    "if num < 0:\n",
    "    print(\"请输入正整数。\")\n",
    "elif num == 1:\n",
    "    print(\"1\")\n",
    "else:\n",
    "    factor = 2\n",
    "    while num > 1:\n",
    "        if num % factor == 0:\n",
    "            print(factor, end=\"\")\n",
    "            num //= factor\n",
    "            if num > 1:\n",
    "                print(\"*\", end=\" \")\n",
    "        else:\n",
    "            factor += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "6.输入一个不多于6位的正整数，要求：一、求它是几位数，二、逆序打印出各位数字。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "num = int(input(\"请输入一个不多于6位的正整数: \"))\n",
    "\n",
    "# 判断位数\n",
    "digit_count = len(str(num))\n",
    "\n",
    "print(f\"{num} 是 {digit_count} 位数\")\n",
    "\n",
    "# 逆序打印各位数字\n",
    "print(\"逆序打印各位数字: \", end=\"\")\n",
    "while num > 0:\n",
    "    print(num % 10, end=\"\")\n",
    "    num //= 10\n",
    "print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "8.求s=a+aa+aaa+aaaa+aa...a的值，其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加)，几个数相加有键盘控制。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = int(input(\"请输入数字a: \"))\n",
    "n = int(input(\"请输入相加的项数n: \"))\n",
    "\n",
    "s = 0\n",
    "temp = 0\n",
    "for i in range(1, n + 1):\n",
    "    temp = temp * 10 + a\n",
    "    s += temp\n",
    "\n",
    "print(f\"结果为: {s}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "10.有一分数序列：2/1，3/2，5/3，8/5，13/8，21/13...求出这个数列的前N项之和，N由键盘输入。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n = int(input(\"请输入项数N: \"))\n",
    "\n",
    "sum_fractions = 0\n",
    "numerator = 2\n",
    "denominator = 1\n",
    "\n",
    "for i in range(n):\n",
    "    sum_fractions += numerator / denominator\n",
    "    new_numerator = numerator + denominator\n",
    "    denominator = numerator\n",
    "    numerator = new_numerator\n",
    "\n",
    "print(f\"前{N}项之和为: {sum_fractions}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "12.一个整数，它加上100后是一个完全平方数，再加上268又是一个完全平方数，请问该数是多少？\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "\n",
    "for i in range(-100, 1000000):  # 可根据实际情况调整范围\n",
    "    num1 = math.sqrt(i + 100)\n",
    "    num2 = math.sqrt(i + 100 + 268)\n",
    "    if int(num1) == num1 and int(num2) == num2:\n",
    "        print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "14.有一对兔子，从出生后第3个月起每个月都生一对兔子，小兔子长到第三个月后每个月又生一对兔子，假如兔子都不死，问每个月的兔子总数为多少？\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "months = int(input(\"请输入要计算的月数: \"))\n",
    "\n",
    "# 初始化前两个月的兔子对数\n",
    "rabbit_pairs = [1, 1]\n",
    "\n",
    "for i in range(2, months):\n",
    "    # 每个月的兔子对数等于前一个月的兔子对数加上两个月前的兔子对数\n",
    "    rabbit_pairs.append(rabbit_pairs[i - 1] + rabbit_pairs[i - 2])\n",
    "\n",
    "print(f\"第{months}个月兔子的总数为{rabbit_pairs[-1] * 2}对\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "16.任意输入一个3x3矩阵，求矩阵对角线元素之和。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "matrix = []\n",
    "print(\"请输入3x3矩阵的元素：\")\n",
    "for i in range(3):\n",
    "    row = list(map(int, input().split()))\n",
    "    matrix.append(row)\n",
    "\n",
    "# 计算主对角线元素之和\n",
    "main_diagonal_sum = 0\n",
    "for i in range(3):\n",
    "    main_diagonal_sum += matrix[i][i]\n",
    "\n",
    "# 计算副对角线元素之和\n",
    "secondary_diagonal_sum = 0\n",
    "for i in range(3):\n",
    "    secondary_diagonal_sum += matrix[i][2 - i]\n",
    "\n",
    "# 输出结果\n",
    "print(\"主对角线元素之和为：\", main_diagonal_sum)\n",
    "print(\"副对角线元素之和为：\", secondary_diagonal_sum)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "18.有n个整数，使其前面各数顺序向后移m个位置，最后m个数变成最前面的m个数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n = int(input(\"请输入整数的个数n: \"))\n",
    "m = int(input(\"请输入移动的位置数m: \"))\n",
    "\n",
    "nums = []\n",
    "print(\"请输入\", n, \"个整数:\")\n",
    "for i in range(n):\n",
    "    nums.append(int(input()))\n",
    "\n",
    "# 进行移动操作\n",
    "nums = nums[-m:] + nums[:-m]\n",
    "\n",
    "print(\"移动后的数组:\")\n",
    "for num in nums:\n",
    "    print(num, end=\" \")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "20.编写一个函数，输入n为偶数时，调用函数求1/2+1/4+...+1/n,当输入n为奇数时，调用函数1/1+1/3+...+1/n(利用指针函数)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sum_even(n):\n",
    "    return sum(1 / i for i in range(2, n + 1, 2))\n",
    "\n",
    "def sum_odd(n):\n",
    "    return sum(1 / i for i in range(1, n + 1, 2))\n",
    "\n",
    "n = int(input(\"请输入一个整数n: \"))\n",
    "\n",
    "if n % 2 == 0:\n",
    "    print(f\"当n为偶数时，1/2 + 1/4 +... + 1/{n} 的和为: {sum_even(n)}\")\n",
    "else:\n",
    "    print(f\"当n为奇数时，1/2 + 1/4 +... + 1/{n} 的和为: {sum_odd(n)}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
